package com.woniuxy.mini;

import com.woniuxy.core.service.ArticleCommentService;
import com.woniuxy.entity.ArticleComment;
import com.woniuxy.enums.ResponseEnum;
import com.woniuxy.util.ResponseData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @ClassName ArticleCommentController
 * @Description
 * @Author nanqiao
 * @Date 2025/7/12 16:43
 * @Version 1.0
 */
@RestController
@RequestMapping("/comments")
public class ArticleCommentController {

    @Autowired
    private ArticleCommentService articleCommentService;
// 获取帖子的评论树
    @GetMapping("/article/{articleId}")
    public ResponseData<List<ArticleComment>> getComments(@PathVariable("articleId") Long articleId) {
        List<ArticleComment> commentTree = articleCommentService.getCommentTreeByArticleId(articleId);
        return ResponseData.ok(commentTree);
    }
// 添加评论
    @PostMapping({"/add"})
    public ResponseData<Boolean> addComment(@RequestBody ArticleComment comment) {
        boolean saved = articleCommentService.save(comment);
        return saved ? ResponseData.ok(true) : ResponseData.fail(ResponseEnum.FAIL);
    }
    @DeleteMapping("/{commentId}")
    public ResponseData<Boolean> deleteComment(@PathVariable("commentId") Long commentId) {
        boolean deleted = articleCommentService.deleteCommentById(commentId);
        return deleted ? ResponseData.ok(true) : ResponseData.fail(ResponseEnum.FAIL);
    }
//     获取评论详情
@GetMapping("/{commentId}")
public ResponseData<ArticleComment> getCommentById(@PathVariable("commentId") Long commentId) {
    ArticleComment comment = articleCommentService.getCommentByIdWithUserInfo(commentId);
    if (comment != null) {
        return ResponseData.ok(comment); // 泛型安全返回
    } else {
        // 使用已有枚举项并手动构造 fail 响应
        return ResponseData.fail(ResponseEnum.PARAM_ERROR.getCode(), "评论不存在");
    }
}


}

